package com.navinfo.collect.library.data.dao.impl;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.sqlite.db.SupportSQLiteDatabase;
import com.navinfo.collect.library.data.entity.Element;
import com.navinfo.collect.library.data.entity.LayerManager;
import com.navinfo.collect.library.data.entity.Project;
import com.navinfo.collect.library.data.entity.TileElement;
import com.tencent.wcdb.database.SQLiteCipherSpec;
import com.tencent.wcdb.room.db.WCDBOpenHelperFactory;
import android.os.AsyncTask;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

@Database(entities = {Element.class, TileElement.class, LayerManager.class, Project.class},version = 1, exportSchema = false)
public abstract class MapLifeDataBase extends RoomDatabase {
    // marking the instance as volatile to ensure atomic access to the variable
    /**
     * 数据库单例对象
     */
    private static volatile MapLifeDataBase INSTANCE;

    /**
     * 要素数据库类
     */
    public abstract IElementDao getElementDao();

    /**
     * 项目数据库类
     */
//    public abstract IProjectDao getProjectDao();

    /**
     * 图层要素数据库类
     */
    public abstract ITileElementDao getTileElementDao();

    /**
     * 图层数据库类
     */

    public abstract ILayerManagerDao getLayerManagerDao();

    /**
     * 项目管理类
     *
     * @return
     */
    public abstract IProjectManagerDao getProjectManagerDao();

    public static MapLifeDataBase getDatabase(final Context context, final String name) {
        if (INSTANCE == null) {
            synchronized (MapLifeDataBase.class) {
                if (INSTANCE == null) {
                    // [WCDB] To use Room library with WCDB, pass a WCDBOpenHelper factory object
                    // to the database builder with .openHelperFactory(...). In the factory object,
                    // you can specify passphrase and cipher options to open or create encrypted
                    // database, as well as optimization options like asynchronous checkpoint.
                    SQLiteCipherSpec cipherSpec = new SQLiteCipherSpec()
                            .setPageSize(1024)
                            .setSQLCipherVersion(3);
                    WCDBOpenHelperFactory factory = new WCDBOpenHelperFactory()
                            .passphrase("123456".getBytes())  // passphrase to the database, remove this line for plain-text
                            .cipherSpec(cipherSpec)               // cipher to use, remove for default settings
                            .writeAheadLoggingEnabled(true)       // enable WAL mode, remove if not needed
                            .asyncCheckpointEnabled(true);        // enable asynchronous checkpoint, remove if not needed

                    INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                            MapLifeDataBase.class, name)

                            // [WCDB] Specify open helper to use WCDB database implementation instead
                            // of the Android framework.
                            .openHelperFactory(factory)

                            // Wipes and rebuilds instead of migrating if no Migration object.
                            // Migration is not part of this codelab.
                            .fallbackToDestructiveMigration()
                            .addCallback(sRoomDatabaseCallback)
                            .build();
                }
            }
        }
        return INSTANCE;
    }

    /**
     * Override the onOpen method to populate the database.
     * For this sample, we clear the database every time it is created or opened.
     * <p>
     * If you want to populate the database only when the database is created for the 1st time,
     * override RoomDatabase.Callback()#onCreate
     */
    private static RoomDatabase.Callback sRoomDatabaseCallback = new RoomDatabase.Callback() {

        @Override
        public void onOpen(@NonNull SupportSQLiteDatabase db) {
            super.onOpen(db);
            // If you want to keep the data through app restarts,
            // comment out the following line.
            new PopulateDbAsync(INSTANCE).execute();
        }
    };

    /**
     * Populate the database in the background.
     * If you want to start with more words, just add them.
     */
    private static class PopulateDbAsync extends AsyncTask<Void, Void, Void> {

        private final ILayerManagerDao mLayerDao;

        PopulateDbAsync(MapLifeDataBase db) {
            mLayerDao = db.getLayerManagerDao();
        }

        @Override
        protected Void doInBackground(final Void... params) {
            // Start the app with a clean database every time.
            // Not needed if you only populate on creation.
            //mDao.deleteAll();
            Log.e("qj", "doInBackground");
            return null;
        }
    }


}
